package com.reactive.springbootreactive.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.reactive.springbootreactive.dao.UserDao;
import com.reactive.springbootreactive.entity.Authority;
import com.reactive.springbootreactive.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController {
    @Autowired
    private UserDao userDao;

    @RequestMapping("abc")
    public Mono<String> abc() {
        return Mono.just("asfdasfas");
    }

    @RequestMapping("auth/abc1")
    public Mono<String> abc1() {
        return Mono.just("asfdasfas");
    }

    @RequestMapping("auth/abc")
    public Mono<String> empty() {
        return Mono.empty();
    }

    @RequestMapping("auth/error")
    public Mono<String> error() {
        return Mono.error(new RuntimeException());
    }

    @RequestMapping("form")
    public Mono<String> form(@RequestBody Map<String, String> param) throws JsonProcessingException {
        return Mono.just(new ObjectMapper().writeValueAsString(param));
    }


    @RequestMapping("hasAdmin")
    @PreAuthorize("hasAuthority('ADMIN')")
    public Mono<String> hasAdmin() {
        return Mono.just("OK");
    }

    @RequestMapping("hasYk")
    @PreAuthorize("hasAuthority('YK')")
    public Mono<String> hasYk() {
        return Mono.just("OK");
    }


    @PostMapping("user")
    public Flux<User> user(@RequestBody List<User> list) {
        List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(Authority.AuthorityEnum.COMMON.name()));
        for (User user : list) {
            if (user.getUsername() == null || user.getPassword() == null || user.getPhone() == null) {
                return Flux.empty();
            }
            user.setAuthorities(authorities);
        }
        Flux<User> insert = userDao.insert(list);
        return insert;
    }

    @GetMapping("user1")
    public Flux<User> userList() {
        System.out.println(Thread.currentThread().getName());
        Flux<User> all = userDao.findAll();
        return all;
    }

    //    Flux<Person> persons = repository.findAll(Sort.by(new Order(ASC, "lastname")));
    @GetMapping("user")
    public Flux<User> userList1(String phone) {
        System.out.println(Thread.currentThread().getName());
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        userDao.findAll(Sort.by(new Sort.Order(Sort.Direction.ASC, "username")));
        Pageable page = PageRequest.of(1, 1, Sort.by("createTime"));
        Flux<User> persons = userDao.findByPhoneOrderByCreateTime(phone, page);
        return persons;
    }

}
